package patientimport;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.bbcare.comm.DateUtil;
import com.bbcare.comm.SeqUtil;
import com.bbcare.followup.plat.entity.PtDevelopstandardData;
import com.bbcare.followup.plat.service.impl.FamilyReportService;

public class ImportGrowthStandards {
	
	public static ApplicationContext applicationContext;
	
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		// 加载配置文件
        String[] tempStrArr = new String[] { "spring-quartz.xml","spring-mvc.xml", "spring-mybatis.xml" };
        applicationContext = new ClassPathXmlApplicationContext(tempStrArr);
        
//		importScoreStandard();
        importPercentStandard();
		
	}

	private static void importPercentStandard() throws FileNotFoundException,
			IOException, Exception {
		File files = new File("F:\\bbcare\\文档\\生长发育数据\\生长发育百分比数据");
		File[] fs = files.listFiles();
		FamilyReportService service = (FamilyReportService) applicationContext.getBean("familyReportService");

		for(File f : fs){
			System.out.println("---"+f.getAbsolutePath());

			List<PtDevelopstandardData> list = new ArrayList<PtDevelopstandardData>();
			String filePath = f.getAbsolutePath();
			String[] names = filePath.substring(filePath.lastIndexOf("\\")+1).split("_");
			if(names.length > 2) {
				FileReader reader = new FileReader(filePath);
		        BufferedReader br = new BufferedReader(reader);
		        String str = br.readLine();
//		        String[] titles = str.split("	");   //FamilyReportService
		        String gender = "F";
		        if(names[1].equals("boys")){
		        	gender = "N";
		        }else if(names[1].equals("girls")){
		        	gender = "F";
		        }
		        String developProjectName = "";
		        String developProjectId = "";
		        if(names[0].equals("bfa")){
		        	developProjectId = "00005";
		        	developProjectName = "年龄别体重";
		        }else if(names[0].equals("hcfa")){
		        	developProjectId = "00003";
		        	developProjectName = "头围";
		        }else if(names[0].equals("lhfa")){
		        	developProjectId = "00002";
		        	developProjectName = "身高";
		        }else if(names[0].equals("wfa")){
		        	developProjectId = "00001";
		        	developProjectName = "体重";
		        }else if(names[0].equals("wfl")){
		        	developProjectId = "00004";
		        	developProjectName = "身长别体重";
		        }else if(names[0].equals("wfh")){
		        	developProjectId = "00006";
		        	developProjectName = "身高别体重";
		        }
		        
    	        while((str = br.readLine()) != null) {
	        		String [] values = str.split("	");
	        		/*Map<String, Object> report = new HashMap<String, Object>();
        			report.put("id",SeqUtil.getSeqNo());
//        			report.put("age",values[0]);
        			report.put("gender",gender);
        			report.put("developProjectName",developProjectName);
        			report.put("developProjectId",developProjectId);
        			report.put("SD4neg",values[1]);
        			report.put("SD3neg",values[2]);
        			report.put("SD2neg",values[3]);
        			report.put("SD1neg",values[4]);
        			report.put("SD0",values[5]);
        			report.put("SD1",values[6]);
        			report.put("SD2",values[7]);
        			report.put("SD3",values[8]);
        			report.put("SD4",values[9]);
        			if(developProjectId.equals("00004")){
        				report.put("length",values[0]);
        			}else{
        				report.put("age",values[0]);
        			}
        			report.put("standardId", "00003");
        			report.put("resource", "手工导入");
        			report.put("createTime",DateUtil.getSysDateA());*/
	        		PtDevelopstandardData data = new PtDevelopstandardData();
	        		data.setCreatetime(DateUtil.getSysDateA());
	        		data.setDevelopprojectId(developProjectId);
	        		data.setDevelopprojectName(developProjectName);
	        		data.setGender(gender);
	        		data.setId(SeqUtil.getSeqNo());
//	        		data.setNumber(number);
	        		data.setResource("手工导入");
	        		data.setSd0(Double.parseDouble(values[11]));
	        		data.setSd1(Double.parseDouble(values[12]));
	        		data.setSd2(Double.parseDouble(values[14]));
	        		data.setSd3(Double.parseDouble(values[16]));
	        		data.setSd4(Double.parseDouble(values[17]));
	        		data.setSd1neg(Double.parseDouble(values[10]));
	        		data.setSd2neg(Double.parseDouble(values[8]));
	        		data.setSd3neg(Double.parseDouble(values[6]));
	        		data.setSd4neg(Double.parseDouble(values[5]));
	        		
	        		data.setP01(Double.parseDouble(values[4]));
	        		data.setP5(Double.parseDouble(values[7]));
	        		data.setP15(Double.parseDouble(values[9]));
	        		data.setP85(Double.parseDouble(values[13]));
	        		data.setP999(Double.parseDouble(values[18]));
	        		data.setP95(Double.parseDouble(values[15]));
	        		
	        		data.setStandardName("WHO最新");
	        		data.setStandardId("00003");
	        		data.setStandardType("00002");
	        		if(developProjectId.equals("00004") || developProjectId.equals("00006")){
	        			data.setLength(Double.valueOf(values[0]));
        			}else{
        				data.setAge(Integer.parseInt(values[0]));
        			}
	        		list.add(data);
    	        }
    	        
    	        br.close();
    		    reader.close();
			}
			service.save(list);
		}
	}
	
	private static void importScoreStandard() throws FileNotFoundException,
	IOException, Exception {
	File files = new File("F:\\bbcare\\文档\\生长发育数据\\生长发育数据");
	File[] fs = files.listFiles();
	FamilyReportService service = (FamilyReportService) applicationContext.getBean("familyReportService");
	
	for(File f : fs){
		System.out.println("---"+f.getAbsolutePath());
	
//		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		String filePath = f.getAbsolutePath();
		String[] names = filePath.substring(filePath.lastIndexOf("\\")+1).split("_");
		
		List<PtDevelopstandardData> list = new ArrayList<PtDevelopstandardData>();
		if(names.length > 2) {
			
			FileReader reader = new FileReader(filePath);
	        BufferedReader br = new BufferedReader(reader);
	        String str = br.readLine();
//	        String[] titles = str.split(" ");   //FamilyReportService
	        String gender = "F";
	        if(names[1].equals("boys")){
	        	gender = "N";
	        }else if(names[1].equals("girls")){
	        	gender = "F";
	        }
	        String developProjectName = "";
	        String developProjectId = "";
	        if(names[0].equals("bfa")){
	        	developProjectId = "00005";
	        	developProjectName = "年龄别体重";
	        }else if(names[0].equals("hcfa")){
	        	developProjectId = "00003";
	        	developProjectName = "头围";
	        }else if(names[0].equals("lhfa")){
	        	developProjectId = "00002";
	        	developProjectName = "身高";
	        }else if(names[0].equals("wfa")){
	        	developProjectId = "00001";
	        	developProjectName = "体重";
	        }else if(names[0].equals("wfl")){
	        	developProjectId = "00004";
	        	developProjectName = "身长别体重";
	        }else if(names[0].equals("wfh")){
	        	developProjectId = "00006";
	        	developProjectName = "身高别体重";
	        }
	        
	        while((str = br.readLine()) != null) {
	    		String [] values = str.split("	");
	    		/*Map<String, Object> report = new HashMap<String, Object>();
				report.put("id",SeqUtil.getSeqNo());
	//			report.put("age",values[0]);
				report.put("gender",gender);
				report.put("developProjectName",developProjectName);
				report.put("developProjectId",developProjectId);
				report.put("SD4neg",values[1]);
				report.put("SD3neg",values[2]);
				report.put("SD2neg",values[3]);
				report.put("SD1neg",values[4]);
				report.put("SD0",values[5]);
				report.put("SD1",values[6]);
				report.put("SD2",values[7]);
				report.put("SD3",values[8]);
				report.put("SD4",values[9]);*/
	    		PtDevelopstandardData data = new PtDevelopstandardData();
        		data.setCreatetime(DateUtil.getSysDateA());
        		data.setDevelopprojectId(developProjectId);
        		data.setDevelopprojectName(developProjectName);
        		data.setGender(gender);
        		data.setId(SeqUtil.getSeqNo());
//        		data.setNumber(number);
        		data.setResource("手工导入");
        		data.setSd0(Double.parseDouble(values[5]));
        		data.setSd1(Double.parseDouble(values[6]));
        		data.setSd2(Double.parseDouble(values[7]));
        		data.setSd3(Double.parseDouble(values[8]));
        		data.setSd4(Double.parseDouble(values[9]));
        		data.setSd1neg(Double.parseDouble(values[4]));
        		data.setSd2neg(Double.parseDouble(values[3]));
        		data.setSd3neg(Double.parseDouble(values[2]));
        		data.setSd4neg(Double.parseDouble(values[1]));
        		
				if(developProjectId.equals("00004") || developProjectId.equals("00006")){
//					report.put("length",values[0]);
					data.setLength(Double.parseDouble(values[0]));
				}else{
//					report.put("age",values[0]);
					data.setAge(Integer.parseInt(values[0]));
				}
				
				data.setStandardName("WHO2013-zcore");
        		data.setStandardId("00003"); //2013标准
        		data.setStandardType("00003");//z_core
        		
        		data.setCreatetime(DateUtil.getSysDateA());
        		list.add(data);
	        }
	        
	        br.close();
		    reader.close();
		}
		service.save(list);
	 }
	}
}
